# 部署参数
$DeployServer = "213server"
# $DeployUser = "ad"  # 远程服务器的用户名
$DeployPath = "/home/ad/phmee/PhmeeHttpApiHostLu"  # 部署路径
$ServiceName = "PhmeeHttpApiHostLu.service"  # systemd 服务名称
$WaitTime = 5  # 重启服务后的等待时间（秒）

# 本地发布路径
$PublishPath = ".\src\PHMEE.HttpApi.Host\bin\Release\net8.0\publish"

# 1. 恢复和发布项目
Write-Host "Restoring and publishing project..."
dotnet restore ./src/PHMEE.HttpApi.Host/PHMEE.HttpApi.Host.csproj
dotnet publish ./src/PHMEE.HttpApi.Host/PHMEE.HttpApi.Host.csproj -c Release -o $PublishPath

# 2. 删除敏感配置文件
Write-Host "Removing appsettings.json..."
Remove-Item "$PublishPath\appsettings.json" -ErrorAction SilentlyContinue

# 3. 创建远程目录（如果不存在）
Write-Host "Ensuring remote directory exists..."
# ssh $DeployUser@$DeployServer "mkdir -p $DeployPath"
ssh $DeployServer "mkdir -p $DeployPath"

# 4. 使用 SCP 传输文件到远程服务器
Write-Host "Copying files to remote server..."
# scp -r "${PublishPath}\*" ${DeployUser}@${DeployServer}:${DeployPath}
scp -r "${PublishPath}\*" ${DeployServer}:${DeployPath}

# 5. 重启远程服务器上的服务
Write-Host "Restarting service on remote server..."
# ssh $DeployUser@$DeployServer "systemctl restart $ServiceName"
ssh $DeployServer "systemctl restart $ServiceName"

# 6. 等待指定秒数后检查服务状态
Write-Host "Waiting for $WaitTime seconds before checking service status..."
Start-Sleep -Seconds $WaitTime

# 7. 检查服务状态
Write-Host "Checking service status..."
# ssh $DeployUser@$DeployServer "systemctl status $ServiceName --no-pager"
ssh $DeployServer "systemctl status $ServiceName --no-pager"

Write-Host "Deployment completed successfully!"
